package fr.labri.harmony.analysis.jsep.results.filter;

import java.io.IOException;
import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public abstract class ResultFilter {
	
	protected void displayList(String title, List<Integer> correct, List<Integer> wrong) {
		PrintStream ps = new PrintStream(System.out);
		ps.println(" --------- "+ title+ " ---------");
		ps.println(" *** CORRECT RULES *** ");
		printStatistics(correct, ps);
		ps.println(" *** WRONG RULES *** ");
		Collections.shuffle(wrong);
		printStatistics(wrong.subList(0, correct.size()), ps);
	}

	protected void printStatistics(List<Integer> distances, PrintStream ps) {
		int sum = 0;
		for (int dist : distances) {
			sum += dist;
		}
		double mean = (double)sum/(double)distances.size();
		double variance = 0D;
		for (int dist : distances) {
			double dm = (double)dist - mean;
			variance += (dm * dm);
		}
		variance = variance/(double)distances.size();
		double deviation = Math.sqrt(variance);
		Collections.sort(distances);
		List<Integer> quartiles = new ArrayList<>();
		quartiles.add(distances.get(distances.size()/4));
		quartiles.add(distances.get(distances.size()/2));
		quartiles.add(distances.get(distances.size()*3/4));
		quartiles.add(distances.get(distances.size()-1));
		ps.println("Size : "+distances.size());
		ps.println("Moyenne : "+mean);
		ps.println("Quartiles : "+quartiles);
		ps.println("Variance : "+variance);
		ps.println("Ecart-Type : "+deviation);
	}
	
	abstract public void computeResult(ResultSet rs) throws SQLException, IOException;
	
}
